package com.yhn.shortlink.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhn.shortlink.common.model.BaseResponse;
import com.yhn.shortlink.common.model.ResultUtils;
import com.yhn.shortlink.project.dto.req.ShortLinkCreateReqDTO;
import com.yhn.shortlink.project.dto.req.ShortLinkPageReqDTO;
import com.yhn.shortlink.project.dto.req.ShortLinkUpdateReqDTO;
import com.yhn.shortlink.project.dto.resp.ShortLinkCreateRespDTO;
import com.yhn.shortlink.project.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.yhn.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.yhn.shortlink.project.service.LinkService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;


/**
 * @author yhn
 * @version 1.0
 * @description
 * @date 2024/7/21 13:34
 */
@RestController
@RequestMapping("/link")
public class LinkController {

    @Resource
    private LinkService linkService;

    /**
     * 新增短链
     * @return 新增短链接信息
     */
    @PostMapping("/add")
    public BaseResponse<ShortLinkCreateRespDTO> addLink(@RequestBody ShortLinkCreateReqDTO shortLinkCreateReqDTO) {
        return ResultUtils.success(linkService.addShortLink(shortLinkCreateReqDTO));
    }

    /**
     * 短链接分页
     * @return 分页信息
     */
    @GetMapping("/page")
    public BaseResponse<IPage<ShortLinkPageRespDTO>> pageLink(ShortLinkPageReqDTO shortLinkPageReqDTO) {
        return ResultUtils.success(linkService.pageShortLink(shortLinkPageReqDTO));
    }

    /**
     * 查询短链接分组数量
     */
    @GetMapping("/count")
    public BaseResponse<List<ShortLinkGroupCountQueryRespDTO>> countLinkGroup(@RequestParam("requestParam") List<String> requestParam) {
        return ResultUtils.success(linkService.listGroupShortLinkCount(requestParam));
    }

    /**
     * 修改短链接功能
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateLink(@RequestBody ShortLinkUpdateReqDTO shortLinkUpdateReqDTO) {
        return ResultUtils.success(linkService.updateShortLink(shortLinkUpdateReqDTO));
    }

    /**
     * 短链接跳转
     */
    @GetMapping("/{short-uri}")
    public BaseResponse<Boolean> redirectLink(@PathVariable("short-uri") String shortUri, HttpServletResponse response, HttpServletRequest request) throws IOException {
        return ResultUtils.success(linkService.redirectLink(shortUri,response, request));
    }

}
